<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
    <el-form-item label="登记日期" prop="registerDataRadius">
        <el-date-picker
          v-model="registerData"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="所属班组" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入所属班组"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="人员类别" prop="dictStaffType">
        <el-select
          v-model="queryParams.dictStaffType"
          placeholder="请选择人员类别"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('config_staff_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
  <div class="btnWrap">
      <div>
        <el-button
          type="primary"
          @click="showImport"
          :loading="uploadLoading"
          v-hasPermi="['salary:accounting-workshop-staff-score:import']"
        >
          数据导入
        </el-button>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['salary:accounting-workshop-staff-score:export']"
        >
          数据导出
        </el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['salary:accounting-workshop-staff-score:create']"
        >
          新增
        </el-button>
        <el-button
          type="primary"
          @click="handleDelect"
          v-hasPermi="['salary:accounting-workshop-staff-score:delect']"
        >
          删除
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
      </div>
    </div>
  <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length?true:false"
      @register="tableoperation"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      :tableHeight="tableHeight"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      >
      <template #action="{ row }">
        <cell>
          <el-button link type="primary"  @click="openForm('update',row.id)" v-hasPermi="['salary:accounting-workshop-staff-score:update']">编辑</el-button> 
        </cell>
      </template>
    </Table>
  </ContentWrap>
  <!-- 上传文件 -->
  <CusUploadFile ref="cusUploadFile" @refresh="getList" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
  <Drawer ref="drawerRef" @success="getList"/>
</template>

<script setup lang="ts" name="AccountingWorkshopStaffScore">
import { getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as AccountingWorkshopStaffScoreApi from '@/api/payroll/shopWagesCalculate/accountingWorkshopStaffScore'
import Drawer from './AccountingWorkshopStaffScoreFormDrawer.vue'
// const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化
let tableTitleList = reactive<CrudSchema[]>([]) // 表头的数据
const loading = ref(true) // 列表的加载中
// const { push } = useRouter()
const registerData =ref([]) // 登记时间
const drawerRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  registerDataRadius: null,
  staffName: null,
  teamName: null,
  dictStaffType: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const uploadLoading = ref(false) //导入加载
const { tableObject, tableMethods } = useTable({
    getListApi: AccountingWorkshopStaffScoreApi.getAccountingWorkshopStaffScorePage, // 分页接口
    defaultParams: queryParams
  })
  // 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (table) => {
  usTableoperation.value = table
}

// 清空多选
const clearSelectData = ()=>{
  usTableoperation.value.selections = []
  usTableoperation.value.clearTableSelect()
}

const getLabelAll = async () => {
    loading.value = true
    try {
      const data = await AccountingWorkshopStaffScoreApi.getAccountingWorkshopStaffScorePage(queryParams)
      const { allSchemas } = useCrudSchemas(data.labels)
      allSchemas.tableColumns.push({
        align: 'left',
        field: 'action',
        label: '操作',
        width: 80
      })
      tableTitleList = allSchemas.tableColumns
      getList()
    } finally {
      loading.value = false
    }
  }

  /** 查询列表 */
  const getTableData = async (val) => {
    tableObject.currentPage = val
    getList()
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
      queryParams.pageNo = 1
      if(registerData.value != null){
        // console.log('registerData.value',registerData.value)
        queryParams.registerDataRadius = registerData.value
        // queryParams.registerDataRadius = JSON.parse(JSON.stringify(registerData.value)).join(',')
      }
    getList()
  }

    /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    registerData.value = null
    queryParams.registerDataRadius=null
    setSearchParams(queryParams)
  }

    /**导入显示 */
  const cusUploadFile = ref()
  const showImport = () => {
    cusUploadFile.value.open(
      '/salary/accounting-workshop-staff-score/get-import-template',
      '/salary/accounting-workshop-staff-score/import'
    )
  }

  /** 编辑/查看操作 */
  const openForm = (type: string, id?: number) => {
    drawerRef.value.open(type,id)
  }

 /** 删除 */
 const handleDelect = async ()=>{
  let idArr = []
  usTableoperation.value.selections.forEach((item)=>{
    idArr.push(item.id)
  })
  console.log(idArr,'idArr')
  await AccountingWorkshopStaffScoreApi.deleteAccountingWorkshopStaffScore(idArr)
  clearSelectData()
  getList()
}

  /** 导出按钮操作 */
  const exportRef = ref()
  const handleExport = () => {
    exportRef.value.open()
  }


  //导出数据
  const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
      fileName: val.name,
      ids: ''
    }
    if (val.range === 'all') {
      params.ids = []
    } else {
      params.ids = usTableoperation.value.selections.map( item => item.id)
    }
    const data = await AccountingWorkshopStaffScoreApi.exportAccountingWorkshopStaffScore(params)
    download.excel(data, val.name)
    clearSelectData()
  }

    /**打开表格表头设置**/
  const tableTitleSet = ref()
  const openTableTitleSet = () => {
    let code = 'AccountingWorkshopStaffScore'
    tableTitleSet.value.open(tableTitleList, code)
  }

  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(async () => {
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
    await getLabelAll()
    getList()
  })
</script>
<style lang="scss" scoped>
  .btnWrap {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
</style>